package android.slc.appbase.utils;

import android.slc.box.core.widget.menu.MenuItemUtils;
import cn.hutool.core.collection.ListUtil;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.stage.Window;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class FastPopupWindow {
    private MenuItemUtils.OnItemClickListener onItemClickListener;
    private final ContextMenu contextMenu;

    private Node anchor;
    private Side side = Side.BOTTOM;
    private double dx;
    private double dy;

    public FastPopupWindow() {
        this(null);
    }

    public FastPopupWindow(MenuItemUtils.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        this.contextMenu = new ContextMenu();
        this.contextMenu.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if (event.getTarget() instanceof MenuItem) {
                    MenuItem menuItem = (MenuItem) event.getTarget();
                    if (FastPopupWindow.this.onItemClickListener != null) {
                        int index = -1;
                        if (menuItem.getUserData() instanceof MenuItemUtils.MenuItemUserDataWarp) {
                            MenuItemUtils.MenuItemUserDataWarp menuItemUserDataWarp = (MenuItemUtils.MenuItemUserDataWarp) menuItem.getUserData();
                            index = menuItemUserDataWarp.getIndex();
                        }
                        FastPopupWindow.this.onItemClickListener.onItemClick(menuItem, index);
                    }
                }
            }
        });
    }

    public ContextMenu getContextMenu() {
        return contextMenu;
    }

    public MenuItemUtils.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(MenuItemUtils.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public Node getAnchor() {
        return anchor;
    }

    public void setAnchor(Node anchor) {
        this.anchor = anchor;
    }

    public Side getSide() {
        return side;
    }

    public void setSide(Side side) {
        this.side = side;
    }

    public double getDx() {
        return dx;
    }

    public void setDx(double dx) {
        this.dx = dx;
    }

    public double getDy() {
        return dy;
    }

    public void setDy(double dy) {
        this.dy = dy;
    }

    public void setItem(String... itemNames) {
        setItem(Arrays.asList(itemNames));
    }

    public void setItem(Collection<String> itemNames) {
        MenuItemUtils.setItem(this.contextMenu.getItems(), itemNames);
    }

    public void setItem(List<MenuItem> menuItemList) {
        MenuItemUtils.setItem(this.contextMenu.getItems(), menuItemList);
    }

    public void addItem(MenuItem... menuItems) {
        MenuItemUtils.addItem(this.contextMenu.getItems(), menuItems);
    }

    public void addItem(List<MenuItem> menuItemList) {
        MenuItemUtils.addItem(this.contextMenu.getItems(), menuItemList);
    }

    //暂时不用
    @Deprecated
    public void show(String[] itemNames) {
        show(ListUtil.toList(itemNames));
    }

    //暂时不用
    @Deprecated
    public void show(Collection<String> itemNames) {
        setItem(itemNames);
    }

    public void show(Node anchor) {
        //this.contextMenu.setStyle("-fx-min-width: 500;");
        this.contextMenu.show(anchor, side, dx, dy);
    }

    public void show(Node anchor, double dx, double dy) {
        //this.contextMenu.setStyle("-fx-min-width: 500;");
        this.show(anchor, side, dx, dy);
    }

    public void show(Node anchor, Side side, double dx, double dy) {
        //this.contextMenu.setStyle("-fx-min-width: 500;");
        this.contextMenu.show(anchor, side, dx, dy);
    }

    public void showScreen(Node anchor, double screenX, double screenY) {
        //this.contextMenu.setStyle("-fx-min-width: 500;");
        this.contextMenu.show(anchor, screenX, screenY);
    }

    public void show(Window owner) {
        this.contextMenu.show(owner);
    }

    public void show(Window ownerWindow, double anchorX, double anchorY) {
        this.contextMenu.show(ownerWindow, anchorX, anchorY);
    }

    public void show() {
        show(anchor, side, dx, dy);
    }

    public void hide() {
        this.contextMenu.hide();
    }

    public void autoHandlerShow() {
        autoHandlerShow(anchor);
    }

    public void autoHandlerShow(Node anchor) {
        autoHandlerShow(anchor, side, dx, dy);
    }

    public void autoHandlerShow(Node anchor, double dx, double dy) {
        autoHandlerShow(anchor, side, dx, dy);
    }

    public void autoHandlerShow(Node anchor, Side side, double dx, double dy) {
        if (this.contextMenu.isShowing()) {
            this.hide();
        } else {
            this.show(anchor, side, dx, dy);
        }
    }

    public void autoHandlerShowScreen(Node anchor, double screenX, double screenY) {
        if (this.contextMenu.isShowing()) {
            this.hide();
        } else {
            this.showScreen(anchor, screenX, screenY);
        }
    }
}
